<template>
  <div>
    <el-card class="box-card">
      <el-form :inline="true" class="demo-form-inline">
        <el-form-item label="用户名">
          <el-input
            v-model="username"
            placeholder="请输入用户名"
          ></el-input>
        </el-form-item>
        <el-form-item label="姓名">
          <el-input
            v-model="name"
            placeholder="请输入学生姓名"
          ></el-input>
        </el-form-item>
        <el-form-item label="班级">
          <el-input
            v-model="lesson"
            placeholder="请输入学生班级"
          ></el-input>
        </el-form-item>

        <el-form-item>
          <el-button type="primary" @click="load(1)">查询</el-button>
          <el-button type="info" @click="reset()">清空</el-button>
        </el-form-item>
      </el-form>

      <!--按钮-->
      <el-row>
        <el-button type="danger" size="medium" @click="deleteByIds"
          >- 批量删除</el-button
        >
        <el-button type="primary" size="medium" @click="add()">+ 新增学生</el-button>
        
        <el-upload
              class="upload-demo"
              action="http://localhost:8080/students/insertBatch"
              :show-file-list= "false"
              :on-success="handFileSuccess"
              style="display: inline-block; margin-left : 10px"
            >
              <el-button type="primary" plain >+ 批量导入学生信息</el-button>
            </el-upload>
      </el-row>
    </el-card>

    <el-table
      ref="multipleTable"
      :data="tableData"
      tooltip-effect="dark"
      style="width: 100%"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55"> </el-table-column>
      <el-table-column prop="id" label="编号"> </el-table-column>
      <el-table-column prop="username" label="用户名"> </el-table-column>
      <el-table-column prop="name" label="姓名"> </el-table-column>
      <el-table-column prop="lesson" label="班级"> </el-table-column>
      <el-table-column prop="password" label="密码"> </el-table-column>

      <el-table-column align="center" label="操作">
        <template slot-scope="scope">
          <el-button type="primary" size="small" @click="update(scope.row)"
            >编辑</el-button
          >
          <el-button
            type="danger"
            size="small"
            @click="deleteById(scope.row.id)"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <el-row>
      <div class="block" style="margin:10px 0">
    <el-pagination
      @current-change="handleCurrentChange"
      :current-page="page"
      :page-sizes="[100, 200, 300, 400]"
      :page-size="pageSize"
      layout="total, prev, pager, next"
      :total="total">
    </el-pagination>
  </div>
    </el-row>

    <el-dialog title="添加学生" :visible.sync="formVisible"  width="40%">
        <el-form :model="student" label-width="80px" style="padding-right : 20px" :rules="rules">
            <el-form-item label="用户名" prop="username">
            <el-input v-model="student.username"></el-input>
            </el-form-item>
            <el-form-item label="学生姓名" prop="name">
            <el-input v-model="student.name"></el-input>
            </el-form-item>
            <el-form-item label="学生班级" prop="lesson">
            <el-input v-model="student.lesson"></el-input>
            </el-form-item>

            <el-form-item label="密码" prop="password">
            <el-input v-model="student.password"></el-input>
            </el-form-item>

            <el-form-item>
            <el-button type="primary" @click="save()">提交</el-button>
            <el-button @click="formVisible = false">取消</el-button>
            </el-form-item>
      </el-form>
    </el-dialog>


  </div>
</template>

<script>
export default {
  name: "AStudentMsg",
  data() {
    return {
      
      tableData:[], // 所有数据
      page : 1, // 当前的页码
      pageSize : 10, // 每页显示的个数
      username : '',
      name :'',
      lesson: '',
      total : 0,
      formVisible : false,
      student : {},
      rules: {
          username: [
            { required: true, message: '请输入账号', trigger: 'blur' },
            { min: 5, max: 15, message: '长度在 5 到 15 个字符', trigger: 'blur' }
          ],
          name: [
            { required: true, message: '请输入学生', trigger: 'blur' },
            { min: 1, max: 15, message: '长度在 1 到 15 个字符', trigger: 'blur' }
          ],
          lesson: [
            { required: true, message: '请输入学生班级', trigger: 'blur' },
            { min: 1, max: 15, message: '长度在 5 到 15 个字符', trigger: 'blur' }
          ],
          password: [
            { required: true, message: '请输入密码', trigger: 'blur' },
            { min: 5, max: 15, message: '长度在 5 到 15 个字符', trigger: 'blur' }
          ]
      },
      ids : [],

    };
  },
  created() {
    this.load();
  },
  methods: {
    handFileSuccess(res){
      // console.log(res);
      if (res.code === 1) {
            this.$message({
            //message: '登陆成功',
            message:res.msg,
            type: 'success'
          });
         this.load(1);
          } else {
            this.$message({
            //message: '登陆失败',
            message: res.msg,
            type: 'error'
          });
          }
    },
    deleteByIds(){
      if(!this.ids.length){
        this.$message.warning('请选择数据！');
        return;
      }

      this.$confirm("确认删除?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      }).then((result) => {
        console.log(result);
        this.$request.delete('/students/'+ this.ids).then((res) => {
          if (res.code === 1) {
            this.$message({
            //message: '登陆成功',
            message:res.msg,
            type: 'success'
          });
          this.load(1);
          } else {
            this.$message({
            //message: '登陆失败',
            message: res.msg,
            type: 'warning'
          });
          }
        }).catch((error) => {
          console.log(error);
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
      }).catch((err) => {
        console.log(err);
      });
    },
    handleSelectionChange(rows) {
      // console.log(rows);
        //let ids = rows.map(v => v.id);
         this.ids = rows.map(v => v.id);
         console.log(this.ids);
    },

    deleteById(id){
       this.$confirm("确认删除?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      }).then((result) => {
        console.log(result);
        this.$request.delete('/students/' + id ).then((res) => {
          if (res.code === 1) {
            this.$message({
            //message: '登陆成功',
            message:res.msg,
            type: 'success'
          });
          this.load(1);
          } else {
            this.$message({
            //message: '登陆失败',
            message: res.msg,
            type: 'warning'
          });
          }
        }).catch((error) => {
          console.log(error);
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
      }).catch((err) => {
        console.log(err);
      });
    },
    save(){ 
        this.$request({
            url : '/students',
            method : this.student.id ? 'put' : 'post',
            data : this.student
        }).then(res => {
            if(res.code === 1){
                this.$message({
                //message: '登陆成功',
                message:res.msg,
                type: 'success'
            });
            this.load(1);
            this.formVisible = false;
            }else{
                 this.$message({
                //message: '登陆失败',
                message: res.msg,
                type: 'warning'
            });
            }
        });
    },
    update(row){ // 编辑
        this.student = JSON.parse(JSON.stringify(row)); // 给student对象赋值
        this.formVisible = true; // 显示弹窗
    },
    add(){ // 新增
        this.student = {}; // 清空表单
        this.formVisible = true; // 显示弹窗
    },
    load(page){ // 分页查询
        if (page) {
            this.page = page;
        }
        this.$request.get('/students',{
            params : {
                page : this.page,
                pageSize : this.pageSize,
                username: this.username,
                name: this.name,
                lesson: this.lesson

            }
        }).then((res) => {
            this.total = res.data.total;
            this.tableData = res.data.rows;
        }).catch((err) => {
            console.log(err);
        });
    },
    handleCurrentChange(page){
      //  this.page = page;
        this.load(page);
    },
    reset(){
        this.username = '';
        this.name = '';
        this.lesson = '';
        this.load(1);
    }
  },
};
</script>

<style>
</style>